import sys
import os
os.chdir(sys.path[0])
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
import statistics
import traceback
import json
import time
import requests
from DataManager import DataManager
from pyredis.redis_helper import RedisHelper, CHANNEL_SPIDER, CHANNEL_API_SERVER


class RadiocacaSpider(object):
    def __init__(self):
        self.database = DataManager()
        self.database.init_shop_dataBase()
        self.redis_helper = RedisHelper(sub_obj=self, auto_init=True, sub_thread_init=True, sub_channel=CHANNEL_SPIDER)
        self.page_index = 1
        self.switch = self.database.get_switch()

    def get_total(self):
        logging.info("get_total: http://market-api.radiocaca.com/nft-sales")
        try:
            response = requests.get("http://market-api.radiocaca.com/nft-sales")
            data = json.loads(response.text)
            return data["total"]
        except:
            traceback.print_exc()
        return 0

    def get_market_place(self, page_size):
        logging.info("get_market_place: http://market-api.radiocaca.com/nft-sales?pageSize=" + str(page_size))
        try:
            response = requests.get("http://market-api.radiocaca.com/nft-sales?pageSize=" + str(page_size), timeout=10)
            data = json.loads(response.text)
            self.save_market_place(data["list"])
            return data["list"]
        except:
            traceback.print_exc()
        return []

    def save_market_place(self, data):
        stime = time.strftime("%Y%m%dT%H%M%SZ", time.localtime())
        self.database.update_market_place(data, stime, len(data))

        # 生成buy_now
        buy_now = {}
        price_limit = self.database.get_price_limit()
        for item in data:
            if item["name"] in price_limit:
                price = float(item["fixed_price"])
                if price < price_limit[item["name"]]:
                    buy_now[item["id"]] = {
                        "name": item["name"],
                        "fixed_price": price,
                        "diff_price": price_limit[item["name"]] - price
                    }
        self.database.add_buy_now(buy_now, stime)
        self.redis_helper.pub_msg("update_buy_now", buy_now, channel=CHANNEL_API_SERVER)

        # 生成统计数据
        classify = {}
        for item in data:
            if item["name"] not in classify:
                classify[item["name"]] = [float(item["fixed_price"])]
            else:
                classify[item["name"]].append(float(item["fixed_price"]))

        stat = {}
        for key, item in classify.items():
            stat[key] = {
                "total": len(item),
                "min": min(item),
                "max": max(item),
                "average": round(statistics.mean(item), 0),
                "median": statistics.median(item)
            }
        self.database.update_market_place_stat(stat, stime)
        self.redis_helper.pub_msg("update_market_place", stat, channel=CHANNEL_API_SERVER)
        self.pub_spider_loginfo("update_market_place: " + str(len(data)))

    # 更新开关回调
    def update_switch(self, msg):
        logging.info(msg)
        self.switch = msg

    # 定时等待或者，暂停
    def delay(self):
        logging.info("start")
        start_time = time.time()
        count = self.switch["spider_delay"] if "spider_delay" in self.switch else 0
        while True:
            if "spider_flag" in self.switch and self.switch["spider_flag"] and time.time() - start_time > count:
                break
            time.sleep(1)
        logging.info("end")

    def pub_spider_loginfo(self, msg):
        loginfo = {
            "time": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
            "msg": msg
        }
        self.redis_helper.pub_msg("pub_spider_loginfo", loginfo, channel=CHANNEL_API_SERVER)


GET_FROM_NETWORK = True
if __name__ == '__main__':
    radiocaca_spider = RadiocacaSpider()
    if GET_FROM_NETWORK:
        while True:
            radiocaca_spider.delay()
            total = radiocaca_spider.get_total()
            if total > 0:
                market_place = radiocaca_spider.get_market_place(total)
                logging.info("total market_place size: " + str(len(market_place)))
            else:
                logging.warning("total market_place size: " + str(total))
    else:
        while True:
            radiocaca_spider.delay()
            with open("market_place", "r") as f:
                data = json.load(f)
                radiocaca_spider.save_market_place(data)



